import { getQureyGterId } from '../methodAPI'

export function generateRandomString(length) {
    let result = '';
    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    const charactersLength = characters.length;

    for (let i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }

    return result;
}

export function getUrlTabId(url, cburl, getInfrt = false) {

    return new Promise((resolve, reject) => {
        if (!url) reject('No url provided')
        // 先获取当店调用背景页面的页面是否为对应的地址

        chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
            // console.log(tabs, 'tabs')

            if (getInfrt && tabs && tabs.length && tabs[0].url.includes(url)) {
                resolve(tabs[0])
                return
            }
            if (!tabs || !tabs.length) {
                getTabIdForWin(url, cburl).then((tabId) => {
                    resolve(tabId)
                })
                return
            }
            if (tabs[0].url.includes(url)) {
                if (!getQureyGterId()) {
                    resolve(tabs[0])
                    return
                }
                if (getQureyGterId().id === tabs[0].id) {
                    resolve(tabs[0])
                    return
                }
            }
            getTabIdForWin(url).then((tabId) => {
                resolve(tabId)
            })

        })
    })
}
let keys = null
export function getTabIdForWin(url, cburl) {
    return new Promise((resolve, reject) => {
        if (!url) reject('No url provided')
        chrome.windows.getAll({ populate: true, windowTypes: ['normal'] }, (windows) => {
            let tabCreterTser = null
            let winData = null
            let onterIds = false
            // console.log(windows, 'windows')
            if (windows && windows.length) {
                for (let i = 0; i < windows.length; i++) {
                    const wins = windows[i];
                    if (wins.tabs) {
                        wins.tabs.forEach((res, index) => {
                            // console.log(res, 'res')
                            if (res.url.includes(url) && res.id === (getQureyGterId() && getQureyGterId().id)) {
                                onterIds = true
                            }
                        })
                        if (onterIds) {
                            break
                        }
                        tabCreterTser = wins.tabs.find((tab => {
                            return tab.url.includes(url)
                        }))
                        if (tabCreterTser) {
                            winData = wins
                            break
                        }
                    }
                }
            }
            if (onterIds) {
                resolve(getQureyGterId())
                return
            }
            if (tabCreterTser) {
                resolve(tabCreterTser)
                // setTimeout(() => {  // 延时 等待脚本加载到抖店后
                //     resolve(tabCreterTser)
                // }, 35000)
                return
            }
            if (!keys) {
                keys = true
                keys = setTimeout(() => {
                    clearTimeout(keys)
                    keys = null
                }, 30000)
                chrome.tabs.create({ url: cburl || 'https://fxg.jinritemai.com/ffa/marketing/coupon/home' }, (id) => {
                    setTimeout(() => { // 延时 等待脚本加载到抖店后
                        // 这里刷新插件
                        resolve(id)
                    }, 35000)
                })
            }
        })
    })

}

export function getTabIdForWinAllUrlTab(url, cburl) {
    return new Promise((resolve, reject) => {
        if (!url) reject('No url provided')
        chrome.windows.getAll({ populate: true, windowTypes: ['normal'] }, (windows) => {
            let tabCreterTser = []
            let winData = null
            if (windows) {
                for (let i = 0; i < windows.length; i++) {
                    const wins = windows[i];
                    if (wins.tabs) {
                        tabCreterTser.push(...wins.tabs.filter((tab => {
                            return tab.url.includes(url)
                        })))

                    }
                }
            }
            if (tabCreterTser && tabCreterTser.length) {
                resolve(tabCreterTser)
                return
            }
            if (!keys) {
                keys = true
                keys = setTimeout(() => {
                    clearTimeout(keys)
                    keys = null
                }, 30000)
                chrome.tabs.create({ url: cburl || 'https://fxg.jinritemai.com/ffa/marketing/coupon/home' }, (id) => {
                    setTimeout(() => {
                        resolve([id])
                    }, 35000)
                })
            }
        })
    })

}

// export function check_url_open(url) {
//     return new Promise((resolve, reject) => {
//         if (!url) reject('No url provided')
//         chrome.windows.getAll({ populate: true, windowTypes: ['normal'] }, (windows) => {
//             console.log(windows)
//         })
//     })


// }